<!-- 支付渠道-->

<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :destroy-on-close="true"
    title="支付渠道信息"
    width="450px"
    @close="emits('closeDialog')"
  >
    <el-form
      ref="dialogFormRef"
      :model="dialogForm"
      :rules="rules"
      :label-width="formLabelWidth"
    >
      <el-form-item label="渠道名称" prop="name">
        <el-input v-model="dialogForm.name" />
      </el-form-item>
      <el-form-item label="渠道名称标识" prop="pay_type">
        <el-input v-model="dialogForm.pay_type" />
      </el-form-item>
      <el-form-item label="渠道图标" :label-width="formLabelWidth">
        <el-upload
          :action="uploadimgUrl"
          :data="{ project_code: 'mobybox' }"
          :show-file-list="false"
          :on-success="handleUploaderImgSuccess"
          :limit="1"
          :headers="TOKENT_HEADER"
          class="avatar-uploader"
        >
          <img
            v-if="dialogForm.iconUrl"
            :src="dialogForm.iconUrl"
            class="avatar"
          />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="国家名称" prop="country">
        <el-input v-model="dialogForm.country" />
      </el-form-item>
      <el-form-item label="国家简称code" prop="country_code">
        <el-input v-model="dialogForm.country_code" />
      </el-form-item>
      <el-form-item label="国家当前币种" prop="country_currency">
        <el-input v-model="dialogForm.country_currency" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="dialogForm.rank" />
      </el-form-item>
      <el-form-item label="id" style="display: none">
        <el-input v-model="dialogForm.id" disabled />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('cancel')">取消</el-button>
        <el-button
          type="primary"
          @click="submit(dialogForm)"
          :loading="btnLoading"
        >
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref } from "vue";
import { uploadimgUrl } from "@/api/system/uploadimg";
import { TOKENT_HEADER } from "@/config/api-constant/system";
const dialogFormRef = ref(null);

defineProps({
  dialogFormVisible: {
    type: Boolean,
    default: false,
  },
  dialogForm: {
    type: Object,
    default: {
      id: "",
      name: "",
      country: "",
      country_code: "",
      country_currency: "",
      rank: "",
      pay_type: "",
      icon: "",
      iconUrl:"",
    },
  },
  btnLoading: {
    type: Boolean,
    default: false,
  },
});
const emits = defineEmits([
  "closeDialog",
  "cancel",
  "submitForm",
  "handleUploaderImgRequest",
]);

const formLabelWidth = "120px";
// 校验
const rules = {
  name: [{ required: true, message: "请输入渠道名称", trigger: "blur" }],
  country: [{ required: true, message: "请输入国家名称", trigger: "blur" }],
  country_code: [
    { required: true, message: "请输入国家简称code", trigger: "blur" },
  ],
  country_currency: [
    { required: true, message: "请输入国家当前币种", trigger: "blur" },
  ],
  pay_type: [
    { required: true, message: "请输入渠道名称标识", trigger: "blur" },
  ],
};

// 提交
const submit = (dialogForm) => {
  dialogFormRef.value
    .validate()
    .then(async () => {
      try {
        emits("submitForm", dialogForm);
      } catch (error) {
        console.log(error);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};
const handleUploaderImgSuccess = (res) => {
  emits("handleUploaderImgRequest", res);
};
</script>
